package com.softline.documentsmall.controller;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.softline.documentsmall.entity.DicVO;
import com.softline.documentsmall.entity.Dictionary;
import com.softline.documentsmall.entity.EntryDetails;
import com.softline.documentsmall.ret.BusinessErrorEnum;
import com.softline.documentsmall.ret.ReturnVO;
import com.softline.documentsmall.ret.ReturnVOUtils;
import com.softline.documentsmall.service.DictionaryService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.*;
import java.util.HashMap;
import java.util.List;

/**
 * @author zhongweicheng
 * @date 2020/8/25 10:11
 */
@Api(value = "字典管理相关接口", tags = {"字典管理相关接口"})
@RestController
@RequestMapping("/dictionary")
@CrossOrigin(allowCredentials = "true", allowedHeaders = "*")
public class DictionaryController {

    @Autowired
    DictionaryService dictionaryService;

    @ApiOperation(value = "首页初始化", notes = "首页初始化")
    @GetMapping(value = "/initIndex")
    public ReturnVO initIndex(@RequestParam(name = "type", defaultValue = "en") String type) {
        return ReturnVOUtils.success(dictionaryService.indexInitByLanguageType(type));
    }

    @ApiOperation(value = "是否专业术语", notes = "是否专业术语")
    @GetMapping(value = "/isProfessional")
    public ReturnVO isProfessional(@RequestParam(name = "value") String value) {
        return ReturnVOUtils.success(dictionaryService.isProfessional(value));

    }

    @ApiOperation(value = "查询词典按照权重", notes = "查询词典按照权重")
    @GetMapping(value = "/search")
    public ReturnVO serach(@RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo, @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize, @RequestParam(name = "type", defaultValue = "en") String type, @RequestParam(name = "value") String value) {
        if (value == null || value.trim() == null) {
            return ReturnVOUtils.businessFail(BusinessErrorEnum.PARAMS_HAS_NULL);
        }
        return ReturnVOUtils.success(dictionaryService.search(type, value, pageNo, pageSize));
    }

    @ApiOperation(value = "点赞排行榜列表页查询", notes = "点赞排行榜列表页查询")
    @GetMapping(value = "/listByThumbsUpRanking")
    public ReturnVO listByRanking(@RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo, @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize,
                                  @RequestParam(name = "type", defaultValue = "en") String type) {
        List<DicVO> dicVOList = dictionaryService.listByThumbsUpRanking(pageNo, pageSize, type);
        Page<DicVO> result = new Page<>();
        Long count = dictionaryService.selectThumbsUpCount(type);
        result.setRecords(dicVOList);
        result.setCurrent(pageNo);
        result.setSize(pageSize);
        result.setTotal(count);
        return ReturnVOUtils.success(result);
    }

    @ApiOperation(value = "术语详情页展示", notes = "术语详情页展示")
    @GetMapping(value = "/getDictionaryDetail")
    public ReturnVO getDictionaryDetail(@RequestParam(name = "dictionaryId") Integer dictionaryId, @RequestParam(name = "type", defaultValue = "en") String type, @RequestParam(name = "userId", required = false) String userId) {
        return ReturnVOUtils.success(dictionaryService.initDictionaryDetail(dictionaryId, type, userId));
    }

    /**
     * 根据输入查询内容 查询相关术语
     *
     * @param value
     * @return
     */
    @ApiOperation(value = "根据输入查询内容 查询相关术语", notes = "根据输入查询内容 查询相关术语")
    @GetMapping(value = "/selectDictionaryByValue")
    public ReturnVO selectDictionaryByValue(@RequestParam(name = "value") String value) {
        List<HashMap<String, Object>> list = dictionaryService.selectDictionaryByValue(value);
        return ReturnVOUtils.success(list);
    }

    /**
     * 有新接口,此接口弃用 根据术语和语种查询语音
     *
     * @param id   主键
     * @param type 语种
     * @return
     * @author wph
     */
    @Deprecated
    @ApiOperation(value = "根据术语和语种查询语音", notes = "根据术语和语种查询语音")
    @GetMapping(value = "/selectAudioByName")
    public ReturnVO selectAudioByName(@RequestParam(name = "type", defaultValue = "en") String type, @RequestParam(name = "id") Integer id) {
        Dictionary dictionary = dictionaryService.selectAudioByName(id, type);
        if (ObjectUtils.isEmpty(dictionary)) {
            return ReturnVOUtils.businessFail(BusinessErrorEnum.FAIL);
        } else {
            return ReturnVOUtils.success(dictionary);
        }
    }

    /**
     * 根据术语和语种查询11种语言名称
     *
     * @param name 术语
     * @param type 语种
     * @return
     * @author wph
     */
    @ApiOperation(value = "根据术语和语种查询11种语言名称", notes = "根据术语和语种查询11种语言名称")
    @GetMapping(value = "/selectAllName")
    public ReturnVO selectAllName(@RequestParam(name = "type", defaultValue = "en") String type, @RequestParam(name = "name") String name) {
        Dictionary dictionary = dictionaryService.selectAllName(name, type);
        if (ObjectUtils.isEmpty(dictionary)) {
            return ReturnVOUtils.businessFail(BusinessErrorEnum.FAIL);
        } else {
            return ReturnVOUtils.success(dictionary);
        }
    }

    /**
     * 根据术语和语种查询术语定义、参考来源
     *
     * @param name 术语
     * @param type 语种
     * @return
     * @author wph
     */
    @ApiOperation(value = "根据术语和语种查询术语定义、参考来源", notes = "根据术语和语种查询术语定义、参考来源")
    @GetMapping(value = "/selectDefinitionAndSource")
    public ReturnVO selectDefinitionAndSource(@RequestParam(name = "type", defaultValue = "en") String type, @RequestParam(name = "name") String name) {
        List<EntryDetails> entryDetails = dictionaryService.selectDefinitionAndSource(name, type);
        if (ObjectUtils.isEmpty(entryDetails)) {
            return ReturnVOUtils.businessFail(BusinessErrorEnum.FAIL);
        } else {
            return ReturnVOUtils.success(entryDetails);
        }
    }

    /**
     * 有新接口,此接口弃用 根据术语id,shuy和语种查询知识图谱
     *
     * @param value        术语名字
     * @param type         语种
     * @param dictionaryId 术语id
     * @return
     * @author wph
     */
    @ApiOperation(value = "根据术语和语种查询知识图谱", notes = "根据术语和语种查询知识图谱")
    @GetMapping(value = "/selectKnowledge")
    public ReturnVO selectKnowledge(@RequestParam(name = "type", defaultValue = "cn") String type, @RequestParam(name = "value") String value, @RequestParam(name = "dictionaryId") Integer dictionaryId) {
        JSONObject classifications = dictionaryService.selectNewKnowledge(type, value, dictionaryId);
        if (ObjectUtils.isEmpty(classifications)) {
            return ReturnVOUtils.businessFail(BusinessErrorEnum.FAIL);
        } else {
            return ReturnVOUtils.success(classifications);
        }
    }

    /**
     * 延伸阅读
     * 根据术语名称查询对应图书和论文
     *
     * @param name 术语名字
     * @author wph
     */
    @ApiOperation(value = "延伸阅读根据术语名称查询对应图书和论文", notes = "延伸阅读根据术语名称查询对应图书和论文")
    @GetMapping(value = "/selectBookArtical")
    public ReturnVO selectBookArtical(@RequestParam(name = "name") String name) {
        JSONObject resultObject = dictionaryService.selectBookArtical(name);
        if (ObjectUtils.isEmpty(resultObject)) {
            return ReturnVOUtils.businessFail(BusinessErrorEnum.FAIL);
        } else {
            return ReturnVOUtils.success(resultObject);
        }
    }

}
